// Created by Roman Novitsky on 25.05.2009


#region Imported Namespaces


using System.Collections.Generic;

using WtfManager.Framework;


#endregion


namespace WtfManager.EditingHistory
{
  public class EditingHistoryService: Service
  {
    #region Fields


    private readonly Stack<Change> _redoStack = new Stack<Change>();
    private readonly Stack<Change> _undoStack = new Stack<Change>();


    #endregion


    #region Constructors/Finalizers


    internal EditingHistoryService()
    {
    }


    #endregion


    #region Properties/Indexers


    public bool CanRedo
    {
      get
      {
        return _redoStack.Count > 0;
      }
    }

    public bool CanUndo
    {
      get
      {
        return _undoStack.Count > 0;
      }
    }


    public override string Description
    {
      get
      {
        return "Contains and manages editing history.";
      }
    }

    public override string Name
    {
      get
      {
        return "Editing History Service";
      }
    }


    #endregion


    #region Methods


    protected override void OnStateChnaged()
    {
    }


    protected override void OnUnload()
    {
    }


    protected override FrameworkComponentState OnInitialize()
    {
      return FrameworkComponentState.Initialized;
    }


    protected override FrameworkComponentState OnLoad()
    {
      return FrameworkComponentState.Loaded;
    }


    public void Do(Change change)
    {
      change.Do();

      _undoStack.Push(change);
      _redoStack.Clear();
    }


    public void Undo()
    {
      if (_undoStack.Count == 0)
      {
        return;
      }

      Change change = _undoStack.Pop();

      change.Undo();

      _redoStack.Push(change);
    }


    public void Redo()
    {
      if (_redoStack.Count == 0)
      {
        return;
      }

      Change change = _redoStack.Pop();

      change.Do();

      _undoStack.Push(change);
    }


    #endregion
  }
}